package com.controller;

import com.bean.*;
import com.bean.MapData;
import com.mvc.ResponseBody;
import com.service.ExpressService;
import com.util.ExpressEnum;
import com.util.TurnUtil;
import com.util.JSONUtil;
import com.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 柏竹
 * @Description: 前台请求交互 （快递）
 * @Date_Created_in: 2021-05-23 8:52
 * @Modified_By: Sans
 * @Project: 快递e栈
 */
public class ExpressController {

    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String, Integer>> data = ExpressService.console();
        Message msg = new Message("控制台数据",0,data);
        if (data.size() <= 0 || data == null) {
            msg.setStatus(-1);
        }
        return JSONUtil.toJSON(msg);
    }
    
    
    @ResponseBody("/express/map.do")
    public String map(HttpServletRequest req, HttpServletResponse resp){
    
        List<Express> expressAll = ExpressService.findExpressAll(false , 0 , 0);
        List<Map<String,Integer>> departureAllSize = ExpressService.departureAllSize();
        
        List<MapData> data = new ArrayList<>();
        List<String> cityList = new ArrayList<>();
        for (Express e : expressAll) {
            String departure = e.getDeparture();
            if (!cityList.contains(departure)){
                cityList.add(departure);
                MapData mapData = null;
                for (Map<String,Integer> map : departureAllSize) {
                    if (map.containsKey(departure)){
                        mapData = new MapData(departure,map.get(departure));
                        break;
                    }
                }
                data.add(mapData);
            }
        }
        Message msg = new Message("地图数据",0,data);
        return JSONUtil.toJSON(msg);
    }
    

    
    @ResponseBody("/express/list.do")
    public String expressList(HttpServletRequest req, HttpServletResponse resp){
        int offset = Integer.parseInt(req.getParameter("offset"));
        int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        
        if (offset == 0){
            offset = 1;
        }
        
        //分页查询数据
        List<Express> list1 = ExpressService.findExpressAll(true , offset , pageNumber);
        List<BootStrapTableExpress> list2 = TurnUtil.setBootStrapTableExpress(list1);
    
        //集合封装
        List<Map<String, Integer>> console = ExpressService.console();
        int total = console.get(0).get("data1_Size");
        //（集合数据 ，总数量）
        ResultData<BootStrapTableExpress> data = new ResultData(list2,total);
        return JSONUtil.toJSON(data);
    }
    
    
    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
    
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        
        Express e = new Express(number,username,userPhone,company, UserUtil.getUserPhone(req.getSession()));
        boolean flag = ExpressService.insert(e);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("录入成功！");
        }else{
            msg.setStatus(-1);
            msg.setResult("录入失败,检查单号是否重复！");
        }
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        List<Express> e = ExpressService.findExpress(ExpressEnum.NUMBER.type , number);
        Message msg ;
        if (e == null){
            msg = new Message("单号不存在",-1,null);
        }else{
            msg = new Message(0,e.get(0));
        }
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userphone = req.getParameter("userphone");
        int status = Integer.parseInt(req.getParameter("status"));
        
        Express newExpress = new Express(number,username,userphone,company,UserUtil.getUserPhone(req.getSession()));
        newExpress.setStatus(status);
    
        boolean update = ExpressService.update(id , newExpress);
        Message msg;
        if (update){
            msg = new Message("修改成功",0,null);
        }else{
            msg = new Message("修改失败,检查单号是否重复！",-1,null);
        }
        return JSONUtil.toJSON(msg);
    }
    
    
    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("expressId"));
        boolean delete = ExpressService.delete(id);
        Message msg;
        if (delete){
            msg = new Message("删除成功",0,null);
        }else{
            msg = new Message("删除失败",-1,null);
        }
        return JSONUtil.toJSON(msg);
    }
    
}
